#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QSound>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //设置标题
    this->setWindowTitle("翻金币主场景");
    this->setFixedSize(320,580);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //点击开始中的退出，实现退出游戏
    connect(ui->actionQuit,&QAction::triggered,this,[=](){
        this->close();
    });

    //开始音效准备
    QSound * startSound = new QSound(":/res/TapButtonSound.wav");
    //startSound->setLoops(-1); //设置循环播放

    //startSound->play();

    //创建出选择关卡的场景
    chooseScene = new ChooseLevelScene;

    //开始按钮
    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->move(this->width()*0.5 - startBtn->width()*0.5,this->height()*0.7);
    startBtn->setParent(this);

    //监听选择关卡场景中的返回自定义信号
    connect(chooseScene,&ChooseLevelScene::chooseSceneBack,this,[=](){
        chooseScene->hide();
        this->setGeometry(chooseScene->geometry());
        this->show();
    });


    connect(startBtn,&MyPushButton::clicked,this,[=](){

        //播放音效
        startSound->play();

        startBtn->zoom1();
        startBtn->zoom2();

        //延时进入
        QTimer::singleShot(100,this,[=](){

            this->hide();//自身隐藏

            //设置选中关卡场景的位置
            chooseScene->setGeometry(this->geometry());

            chooseScene->show();
        });

    });

}

MainScene::~MainScene()
{
    delete ui;
}


void MainScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;

    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5,pix.height()*0.5);

    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

}
